package com.ddpyjqtd.thread.threadstop;

import java.util.concurrent.TimeUnit;

/**
 * 
 * @Description: 线程停止
 * 
 * @author: ddpyjqtd
 * @Date: 2019年5月13日
 *
 */
public class ThreadInterrupt implements Runnable {
	@Override
	public void run() {
		System.out.println("线程开始");
		while (!Thread.currentThread().isInterrupted()) {
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
				System.out.println("获取到中断信号，停止线程.");
				break;
			}
		}
		System.out.println("线程结束");
	}

	public static void main(String[] args) {
		Runnable runnable = new ThreadInterrupt();
		Thread thread = new Thread(runnable);
		thread.start();
		System.out.println("这里是主线程");
		try {
			TimeUnit.SECONDS.sleep(3);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("准备关闭子线程");
		thread.interrupt();

	}
}
